﻿using System;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Share;
using ASPNETVN.PORTAL.Components.UI;
using QLC.Modules.TDHS.Components;

namespace QLC.Modules.TDHS.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = LinkButtonSave;
            this.ButtonDelete = LinkButtonDelete;
            this.ButtonCancel = LinkButtonCancel;

            this.Update += AddEditItem_Update;
            this.Delete += AddEditItem_Delete;
            base.OnInit(e);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Components.Item.DeleteItem(this.ItemID);
                this.ReturnToReferrerPage();
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            try
            {
                Components.Item item = new Components.Item();
                if (this.ItemID > 0)
                {
                    item = Components.Item.Load(this.ItemID);
                }
                item.ModuleID = this.ModuleID;
                item.CategoryID = Convert.ToInt32(DropDownListCategory.SelectedValue);
                item.RegisterNumber = TextBoxRegisterNumber.Text;
                item.Status = DropDownListStatus.SelectedValue;
                item.RegisterName = TextBoxRegisterName.Text;
                item.IdentityNumber = TextBoxIdentityNumber.Text;
                item.Email = TextBoxEmail.Text;
                item.Phone = TextBoxPhone.Text;
                item.Address = TextBoxAddress.Text;
                item.Reply = TextBoxReply.Text;
                item.ReceivedDate = Convert.ToDateTime(TextBoxRegisterDate.Text);
                item.FinishedDate = Convert.ToDateTime(TextBoxFinishedDate.Text);

                if (this.ItemID == 0)
                {
                    this.ItemID = item.Insert();
                }
                else
                {
                    item.Update();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            string url = HttpUrlBuilder.BuildShareUrl(this.ModuleID, this.ItemID, "UploadFile");
            Response.Redirect(url);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                Category.BuildDropDownListControl(ref DropDownListCategory, this.ModuleID);
                if (this.ItemID > 0)
                {
                    Item item = Item.Load(this.ItemID);
                    if (item != null)
                    {
                        DropDownListCategory.SelectedValue = item.CategoryID.ToString();
                        TextBoxRegisterNumber.Text = item.RegisterNumber;
                        DropDownListStatus.SelectedValue = item.Status;
                        TextBoxRegisterName.Text = item.RegisterName;
                        TextBoxIdentityNumber.Text = item.IdentityNumber;
                        TextBoxEmail.Text = item.Email;
                        TextBoxPhone.Text = item.Phone;
                        TextBoxAddress.Text = item.Address;
                        TextBoxReply.Text = item.Reply;
                        TextBoxRegisterDate.Text = item.ReceivedDate.ToShortDateString();
                        TextBoxFinishedDate.Text = item.FinishedDate.ToShortDateString();
                    }
                }
            }
        }
    }
}